<template>
    <a-modal
        v-model:visible="showModal"
        :title="title"
        :width="520"
        :destroyOnClose="true"
        :maskClosable="false"
        :confirmLoading="loading"
        @ok="onSubmit"
        @cancel="handleCancle"
    >
        <a-form class="dept-model" layout="horizontal" :model="formState" :rules="rules" ref="formRef">
            <a-form-item :label="t('yl.agentName')" name="name">
                <a-input v-model:value="formState.name" :placeholder="t('yl.agentName')" />
            </a-form-item>
            <a-form-item :label="t('yl.contactPerson')" name="contactName">
                <a-input v-model:value="formState.contactName" :placeholder="t('yl.contactPerson')" />
            </a-form-item>
            <a-form-item :label="t('yl.account')" name="account">
                <a-input v-model:value="formState.account" :placeholder="t('yl.account')" />
            </a-form-item>
            <a-form-item :label="t('yl.cellphone')" name="contactTel">
                <a-input v-model:value="formState.contactTel" :placeholder="t('yl.cellphone')" />
            </a-form-item>
            <a-form-item :label="t('yl.password')" name="password" v-if="!formState.id">
                <a-input v-model:value="formState.password" :placeholder="t('yl.password')" />
            </a-form-item>
            <a-form-item :label="t('yl.email')" name="email">
                <a-input v-model:value="formState.email" :placeholder="t('yl.email')" />
            </a-form-item>
            <a-form-item :label="t('yl.province')">
                <a-cascader
                    :options="country"
                    :fieldNames="{ label: 'name', value: 'name', children: 'countryRegionDtos' }"
                    :props="CountryData"
                    v-model:value="formState.provinceList"
                    :placeholder="t('yl.province')"
                />
                <!-- <a-input v-model:value="formState.province" :placeholder="t('yl.province')" /> -->
            </a-form-item>
            <a-form-item :label="t('yl.afterSales')" name="afterSalesSpecialis">
                <a-input v-model:value="formState.afterSalesSpecialis" :placeholder="t('yl.afterSales')" />
            </a-form-item>
            <a-form-item :label="t('yl.address')">
                <a-input v-model:value="formState.address" :placeholder="t('yl.address')" />
            </a-form-item>
        </a-form>
    </a-modal>
</template>
<script lang="ts">
import { computed, defineComponent, reactive, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import _ from 'lodash';
import ylTreeselectVue from '@/components/ylTreeselect.vue';

interface DeptState {
    name: string;
    contactName: string;
    account: string;
    contactTel: string;
    address: string;
    afterSalesSpecialis: string;
    systype: string;
    password: string;
    provinceList: any[];
    provinceName: string | number;
    provinceId: string | number;
    cityName: string | number;
    cityId: string | number;
    countyName: string | number;
    countyId: string | number;
    areaName: string | number;
    areaId: string | number;
    id: string | number;
    email: string | number;
}

// 表单校验规则
const initRules = (t: fn) => {
    return {
        name: [{ required: true, message: t('yl.productionQuantity'), trigger: 'blur' }],
        contactName: [{ required: true, message: t('yl.equipmentModel'), trigger: 'blur' }],
        account: [{ required: true, message: t('yl.productionQuantity'), trigger: 'blur' }],
        contactTel: [{ required: true, message: t('yl.equipmentModel'), trigger: 'blur' }],
        afterSalesSpecialis: [{ required: true, message: t('yl.productionQuantity'), trigger: 'blur' }],
        password: [{ required: true, message: t('yl.equipmentModel'), trigger: 'blur' }],
        email: [{ required: true, message: t('yl.email'), trigger: 'blur' }],
    };
};

export default defineComponent({
    name: 'roleModel',
    components: {
        ylTreeselectVue,
    },
    props: {
        visible: {
            type: Boolean,
            default: () => false,
        },

        title: {
            type: String,
        },
        loading: {
            type: Boolean,
            default: () => false,
        },
        dept: {
            type: Object,
        },
        CountryData: {
            type: Object,
        },
        countryRegionList: {
            type: Array,
        },
    },
    emits: ['submit', 'cancle'],
    setup(props, { emit }) {
        const { t } = useI18n();

        const showModal = computed(() => props.visible);
        const country = ref<any>([]);

        const CountryData = {
            value: 'id',
            label: 'name',
            children: 'countryRegionDtos',
        };

        const formState: DeptState = reactive({
            name: '', // 设备编号
            contactName: '',
            account: '',
            contactTel: '',
            address: '',
            afterSalesSpecialis: '',
            password: '',
            systype: 'A',
            email: '',
            provinceList: [],
            provinceName: '',
            provinceId: '',
            cityName: '',
            cityId: '',
            countyName: '',
            countyId: '',
            areaName: '',
            areaId: '',
            id: '',
        });
        watch(
            () => props.countryRegionList,
            (list) => {
                country.value = list;
            }
        );
        // 编辑时赋值
        watch(
            () => props.dept,
            (dept) => {
                console.log(dept);
                // if (_.isEmpty(dept)) return;
                const {
                    name,
                    email,
                    contactName,
                    account,
                    contactTel,
                    address,
                    afterSalesSpecialis,
                    password,
                    areaName,
                    provinceName,
                    cityName,
                    countyName,
                    id,
                } = dept as NormalObj;

                formState.name = name;
                formState.contactName = contactName;
                formState.account = account;
                formState.contactTel = contactTel;
                formState.address = address;
                formState.afterSalesSpecialis = afterSalesSpecialis;
                formState.password = password;
                formState.email = email;
                formState.id = id;
                if (areaName) {
                    formState.provinceList = (provinceName + ',' + cityName + ',' + countyName + ',' + areaName).split(',');
                } else if (countyName) {
                    formState.provinceList = (provinceName + ',' + cityName + ',' + countyName).split(',');
                } else if (provinceName && cityName) {
                    formState.provinceList = (provinceName + ',' + cityName).split(',');
                } else {
                    formState.provinceList = [];
                }
                formState.systype = 'A';
            }
        );

        // 表单校验
        const rules = initRules(t);
        const formRef = ref();
        // 提交
        const onSubmit = () => {
            formRef.value.validate().then(() => {
                let pathList: any = [];
                if (formState.provinceList.length) {
                    TreeFun(country.value, pathList);
                    console.log(pathList.length);
                    if (pathList.length) {
                        formState.provinceName = pathList[0].name;
                        formState.provinceId = pathList[0].id;
                        formState.cityName = pathList[1].name;
                        formState.cityId = pathList[1].id;
                        if (pathList.length == 3) {
                            formState.countyName = pathList[2].name;
                            formState.countyId = pathList[2].id;
                        } else if (pathList.length == 4) {
                            formState.areaName = pathList[3].name;
                            formState.areaId = pathList[3].id;
                        }
                    }
                }
                emit('submit', formState);
            });
        };
        // 取消
        const handleCancle = () => {
            return emit('cancle');
        };
        const TreeFun = (list: any, pathList: any) => {
            for (let i = 0; i < list.length; i++) {
                for (let j = 0; j < formState.provinceList.length; j++) {
                    if (list[i].name == formState.provinceList[j]) {
                        pathList.push({ id: list[i].id, name: list[i].name });
                        if (list[i].countryRegionDtos && list[i].countryRegionDtos.length) {
                            TreeFun(list[i].countryRegionDtos, pathList);
                        }
                    }
                }
            }
        };
        return {
            formRef,
            showModal,
            formState,
            t,
            rules,
            onSubmit,
            handleCancle,
            country,
            TreeFun,
        };
    },
});
</script>
<style lang="scss" scoped>
.dept-model {
    @include flexWrap;
    width: 100%;
    .ant-form-item {
        @include flex;
        width: 50%;
        margin-right: 0;
        padding-right: $width10;
        margin-bottom: $width8;
        :deep .ant-form-item-label {
            flex-basis: $formLabel;
        }
        :deep .ant-form-item-control-wrapper {
            flex: 1;
        }
        :deep .ant-form-item-children,
        :deep .ant-input-number {
            width: 100%;
        }
        &.full-width {
            width: 100%;
        }
    }
}
</style>
